package com.budwk.app.sqfwV2.service.centuryCity;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_servicerecord;
import com.budwk.app.sqfw.services.SqfwServicerecordService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGenerator.impl.SqfwTemplateExcelCommonService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwV2.dto.dto.OldCareServiceStationPdfDTO;
import com.budwk.app.sqfwV2.generator.centuryCity.CenturyCityOldCareServicePatrolExcelGenerator;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;

@IocBean
public class OldCareServiceStationExcelService extends SqfwTemplateExcelCommonService {

    @Inject
    private SqfwServicerecordService sqfwServicerecordService;

    public List<GenerateExcelUrlVO> createExcel(List<String> sqfwIdList) {
        ExcelCreateConfig config = new ExcelCreateConfig();
        config.setFileIdentity(DownLoadFileNameEnum.OLD_CARE_SERVICE_PATROL.getCode());
        config.setListAtLastRowFlag(false);
        config.setRowHeight(56);    // 这个值从excel模板中获取

        return generateExcel(Lists.newArrayList(config),
                SecurityUtil.getUserId(), sqfwIdList);
    }

    @Override
    public List<GenerateExcelUrlVO> generateExcel(List<ExcelCreateConfig> excelConfigList, String userId, List<String> sqfwIdList) {
        return super.generateExcel(excelConfigList, userId, sqfwIdList);
    }

    public List<GenerateExcelUrlVO> generateExcelForAdmin(List<ExcelCreateConfig> excelConfigList, String userId, List<String> formSummaryNewIdList) {
        return super.generateExcelForAdmin(excelConfigList, userId, formSummaryNewIdList);
    }

    /**
     * 允许自定义生成excel的原材料，
     *
     * @param baseExcelParam 生成excel提供的原材料
     * @param userId         用户id
     * @param sqfwIdList     社区服务id列表
     */
    @Override
    protected void customerPopularBaseExcelParam(BaseExcelParamDTO baseExcelParam, String userId, List<String> sqfwIdList) {
        if (sqfwIdList != null && sqfwIdList.size() > 0) {
            String sqfwId = sqfwIdList.get(0);
            Sqfw_servicerecord serviceRecord = sqfwServicerecordService.fetch(sqfwId);
            if (serviceRecord != null) {
                baseExcelParam.setProvince(serviceRecord.getProvince());
                baseExcelParam.setCity(serviceRecord.getCity());
                baseExcelParam.setDistrict(serviceRecord.getDistrict());
                baseExcelParam.setCounty(serviceRecord.getCounty());
                baseExcelParam.setCommunity(serviceRecord.getCommunity());
            }
        }
    }

    @Override
    protected void customerPopularBaseExcelParamForAdmin(BaseExcelParamDTO baseExcelParam, String userId, List<String> formSummaryNewIdList) {

    }

    /**
     * 选择使用哪一个excel生成器来生成excel，默认一个Excel会对应一个Generator
     *
     * @return 结果
     */
    @Override
    protected List<ExcelGeneratorConfig> getExcelCodeGeneratorConfig(List<ExcelCreateConfig> excelConfigList, BaseExcelParamDTO baseExcelParam) {
        List<ExcelGeneratorConfig> generatorList = Lists.newArrayList();
        ExcelGeneratorConfig excelConfig = new ExcelGeneratorConfig();
        excelConfig.setGenerator(new CenturyCityOldCareServicePatrolExcelGenerator());
        excelConfig.setExcelCreateConfig(excelConfigList.get(0));
        generatorList.add(excelConfig);
        return generatorList;
    }

    @Override
    protected AbstractCustomerPdfCreateField parseParamData(String paramDataStr) {
        return JSONObject.parseObject(paramDataStr, OldCareServiceStationPdfDTO.class);
    }

    /**
     * 第五张表中的需要的类型，详情见 {@link com.budwk.app.sqfw.constants.SqfwFormSummaryConstants}
     *
     * @return 返回数据
     */
    @Override
    protected String getSqfwFormSummaryType() {
        return SqfwFormSummaryConstants.old_care_service_patrol;
    }


    /**
     * wzy 2024-2-20 创建excel合集
     *
     * @param sqfwIdList id合集
     * @return 结果
     */
    public String generateExcelColForApp(List<String> sqfwIdList) {
        if (sqfwIdList == null || sqfwIdList.size() == 0) return "";
        List<GenerateExcelUrlVO> generateExcelUrlVOS = createExcel(sqfwIdList);
        return generateExcelUrlVOS.get(0).getExcelUrl();
    }
}
